<template>
  <el-dialog
    :title="mode === 'add' ? '新增字典' : '编辑字典'"
    :model-value="visible"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    class="dictionary-dialog"
  >
    <template #header>
      <div class="dialog-header">
        <span class="dialog-title">{{ mode === 'add' ? '新增字典' : '编辑字典' }}</span>
      </div>
    </template>
    
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="140px"
      label-position="right"
    >
      <el-form-item label="字典类型名称" prop="value">
        <el-input
          v-model="form.value"
          placeholder="请输入字典类型名称"
          style="width: 300px"
        />
      </el-form-item>
      
      <el-form-item label="字典类型键" prop="typeKey">
        <el-input
          v-model="form.typeKey"
          :disabled="mode === 'edit'"
          placeholder="请输入字典类型键"
          style="width: 300px"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
          active-text="启用"
          inactive-text="停用"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注（可选）"
          :rows="3"
          style="width: 300px"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">
          {{ mode === 'add' ? '确定' : '更新' }}
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: Boolean,
  mode: { type: String, default: 'add' }, // 'add' | 'edit'
  data: { type: Object, default: () => ({}) }
})
const emits = defineEmits(['update:visible', 'submit'])

const formRef = ref(null)
const form = ref({
  value: '',
  typeKey: '',
  status: 1,
  remark: ''
})

const rules = {
  value: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  typeKey: [{ required: true, message: '请输入字典类型', trigger: 'blur' }]
}

watch(
  () => props.visible,
  (val) => {
    if (val) {
      // 打开弹窗时初始化表单
      if (props.mode === 'edit') {
        form.value = { ...props.data }
      } else {
        form.value = { value: '', typeKey: '', remark: '' }
      }
    }
  }
)

function handleClose() {
  emits('update:visible', false)
}

function handleSubmit() {
  formRef.value.validate((valid) => {
    if (valid) {
      emits('submit', { ...form.value })
      handleClose()
    }
  })
}
</script>

<style scoped lang="scss">
.dictionary-dialog {
  :deep(.el-dialog__header) {
    padding: 20px 20px 0;
    border-bottom: 1px solid #f0f0f0;
  }
  
  :deep(.el-dialog__body) {
    padding: 30px 20px;
  }
  
  :deep(.el-dialog__footer) {
    padding: 0 20px 20px;
    border-top: 1px solid #f0f0f0;
  }
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-title {
  font-size: 18px;
  font-weight: normal !important;
  color: #333;
  text-align: center;
  width: 100%;
  display: block;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}

:deep(.el-textarea__inner) {
  resize: vertical;
}

:deep(.el-dialog__title) {
  font-weight: normal !important;
  text-align: center;
  width: 100%;
  display: block;
}
</style>